package servlet.MaterialServlet;

import dao.MaterialDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/MaterialDeleteServlet")
public class MaterialDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        MaterialDao dao = new MaterialDao();
        boolean r = false;
        try {
            r = dao.deleteMaterialById(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (r) {
            //String url_next = "person_list.jsp?page=" + curpage + "&name=" + name + "&worknumber=" + worknumber + "&department=" + department + "&worktime=" + worktime + "&profession=" + profession;
            request.getRequestDispatcher("MaterialListServlet").forward(request, response);
        } else {
            response.setHeader("refresh", "3;material_list.jsp");
        }
    }
}
